#include "songlist2.h"
#include "ui_songlist2.h"
#include"tcpclient.h"
songlist2::songlist2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::songlist2)
{
    ui->setupUi(this);
    arr<<ui->songlist_1<<ui->songlist_2<<ui->songlist_3<<ui->songlist_4<<ui->songlist_5
       <<ui->songlist_6<<ui->songlist_7<<ui->songlist_8;
    ui->widget->setFixedHeight(height()/7);

    arr[0]->set(":/new/prefix1/res/meirtuijian.png","每日推荐");
    arr[1]->set(":/new/prefix1/res/222.png","吹吹晚风从前听\n故事的人");
    arr[2]->set(":/new/prefix1/res/333.png","全网最火超热门\n歌曲推荐(持续更新)");
    arr[3]->set(":/new/prefix1/res/444.png","痛到心碎的虐心歌曲");
    arr[4]->set(":/new/prefix1/res/555.png","最后还是把你归入人海");
    arr[5]->set(":/new/prefix1/res/666.png","听你爱的骄傲的猫\n|华语私人雷达");
    arr[6]->set(":/new/prefix1/res/777.png","我曾经爱过她呢");
    arr[7]->set(":/new/prefix1/res/888","「温柔翻唱」好听的歌\n从来没有完整版");

    connect(arr[0],&musiclist::mouseDoubleClick,this,&songlist2::DailyRecommendation);
}

songlist2::~songlist2()
{
    delete ui;
}

void songlist2::DailyRecommendation()
{
    //连接服务器
    tcpclient*client=new tcpclient();
    //usrnameid:{1373474316}\r\npassword:{137347}\r\nRequestMusicList:{每日歌单}\r\n
    //usrnameid:{1373474316}\r\npassword:{137347}\r\nRequestMusicList:{每日歌单}\r\n
    //发送请求报文
    client->write("usrnameid:{}\\r\\npassword:{}\\r\\nRequestMusicList:{每日推荐}\\r\\n");
    client->send();
    connect(client->m_socket,&QTcpSocket::readyRead,[=](){
        int ret=1;
        while(ret > 0)
        {
           QByteArray arr;
           arr=client->m_socket->readAll();
           client->Receiving.append(arr);
           ret=arr.size();
        }
    });

    connect(client->m_socket,&QTcpSocket::disconnected,[=](){
        client->m_socket->close();
        list<QString>list;
        if(client->Receiving.data()!=nullptr){
            //将数据
            QString array;
            int pos=0,end=0;
            array=client->Receiving;
            while(1){
               end=array.indexOf('|',pos);
               if(end==-1)
               {
                   break;
               }
               QString s=array.mid(pos,end-pos);
               pos=end+1;
               list.push_back(s);
            }
            emit showmusiclist(list);   //发送歌曲列表信息 信号
        }
        else   //没有找到歌曲
        {

        }


    });
}
